# -*- encoding=utf8 -*-
__author__ = "10511.ZNPT.000"

from airtest.core.api import *
import time
auto_setup(__file__)
from airtest.core.settings import Settings as ST
import tkinter as tk
ST.CVSTRATEGY = ["tpl", "sift"]
ST.FIND_TIMEOUT_TMP = 1
ST.SAVE_IMAGE = False
# connect_device("android://127.0.0.1:5037/emulator-5554")
count = 10 ##循环次数
pos = 1    ##模式选择，1表示国战；2表示军争8人，适合刷反贼、忠臣；3表示军争5人，适合刷内奸、主公
def touch_back():
    ST.FIND_TIMEOUT_TMP = 1.5
    while exists(Template(r"tpl1738736529990.png", threshold=0.8, record_pos=(0.467, -0.244), resolution=(1600, 900))):
        touch((1550,60)) # 返回键的位置
        sleep(0.3)
    ST.FIND_TIMEOUT_TMP = 2
def guozhan():
    global count
    while count>0:
        count -=1
        touch_back()
        touch((314,452)) # 进入经典场
        sleep(1)
        touch((307,829))
        sleep(1)
        touch((297,563),times=2)# 国战
        sleep(1)
        touch((1163,180))
        sleep(11)
        for i in range(16):
            touch((i*100,250),times=2)
            sleep(0.1)
        sleep(1)
        touch((1100,593))
        ST.FIND_TIMEOUT_TMP = 1
        sleep(1)
        touch((1550,60))
        sleep(0.5)
        touch((1409,116),times=2)
        while True:
            touch((1070,655),times=2)
            if exists(Template(r"tpl1734453121734.png", threshold=0.85, record_pos=(-0.214, -0.182), resolution=(1600, 900))):
                sleep(2)
                touch((42,98),times=2)
                sleep(1)
                touch((42,98),times=2)
                sleep(1)
                break
        touch((42,98),times=2)
        ST.FIND_TIMEOUT_TMP = 2
        sleep(1.5)
        
def junzheng8():
    global count
    while count>0:
        count-=1
        touch((314,452)) # 进入经典场
        sleep(1)
        touch(Template(r"tpl1748012411382.png", record_pos=(-0.307, 0.236), resolution=(1600, 900)))
        sleep(1)
        touch((300,473),times=2)
        sleep(0.5)
        touch(Template(r"tpl1748012537592.png", record_pos=(0.229, -0.156), resolution=(1600, 900)))
        sleep(1)
        # 演练过快，出现回到演练场的bug，处理一下
        while exists(Template(r"tpl1734422716170.png", record_pos=(-0.101, 0.044), resolution=(1600, 900))):
            touch(Template(r"tpl1734422716170.png", record_pos=(-0.101, 0.044), resolution=(1600, 900)))
            sleep(1)
            touch((1163,180))
        sleep(8.5)
        num = -1
        while exists(Template(r"tpl1734160853270.png", record_pos=(-0.104, 0.206), resolution=(1600, 900))):
            num+=1
            touch((8,127),times=2)
            sleep(0.5)
            if num == 0:#单选将
                touch((188,617),times=2) 
            elif num == 2:#2.3选将
                touch((188,617)) 
                sleep(1)
                touch((620,400),times=2)
            elif num == 1:#4选将
                touch((188,617))
                sleep(1)
                touch((400,400),times=2)           
            else:
                num = -1
            sleep(0.5)
        sleep(1)
        while True:
            touch((1070,655),times=2)
            if exists(Template(r"tpl1734453121734.png", threshold=0.85, record_pos=(-0.214, -0.182), resolution=(1600, 900))):
                sleep(2)
                touch((42,98),times=2)
                sleep(1)
                touch((42,98),times=2)
                sleep(1)
                break
        touch((42,98),times=2)
        sleep(1.5)
        touch_back()

def junzheng5():
    global count
    while count>0:
        count-=1
        touch((314,452)) # 进入经典场
        sleep(1)
        touch(Template(r"tpl1748012411382.png", record_pos=(-0.307, 0.236), resolution=(1600, 900)))
        sleep(1)
        touch((300,352),times=2)
        sleep(0.5)
        touch(Template(r"tpl1748012537592.png", record_pos=(0.229, -0.156), resolution=(1600, 900)))
        sleep(1)
        # 演练过快，出现回到演练场的bug，处理一下
        while exists(Template(r"tpl1734422716170.png", record_pos=(-0.101, 0.044), resolution=(1600, 900))):
            touch(Template(r"tpl1734422716170.png", record_pos=(-0.101, 0.044), resolution=(1600, 900)))
            sleep(1)
            touch((1163,180))
        sleep(8.5)
        num = -1
        while exists(Template(r"tpl1734160853270.png", record_pos=(-0.104, 0.206), resolution=(1600, 900))):
            num+=1
            touch((8,127),times=2)
            sleep(0.5)
            if num == 0:#单选将
                touch((188,617),times=2) 
            elif num == 2:#2.3选将
                touch((188,617)) 
                sleep(1)
                touch((620,400),times=2)
            elif num == 1:#4选将
                touch((188,617))
                sleep(1)
                touch((400,400),times=2)           
            else:
                num = -1
            sleep(0.5)
        sleep(1)
        while True:
            touch((1070,655),times=2)
            if exists(Template(r"tpl1734453121734.png", threshold=0.85, record_pos=(-0.214, -0.182), resolution=(1600, 900))):
                sleep(2)
                touch((42,98),times=2)
                sleep(1)
                touch((42,98),times=2)
                sleep(1)
                break
        touch((42,98),times=2)
        sleep(1.5)
        touch_back()
def save_config():
    global count,pos_entity,pos,count_entity
    count = int(count_entity.get())
    pos = int(pos_entity.get())
    if pos == 1:
        print("开始国战托管"+str(count)+"次")
    elif pos == 2:
        print("开始军争8人托管"+str(count)+"次")
    elif pos == 3:
        print("开始军争5人托管"+str(count)+"次")
    root.destroy()
def on_closing():
    root.destroy()  # 关闭窗口
    sys.exit(0)     # 终止程序
root = tk.Tk()
root.title("游戏配置工具")
root.protocol("WM_DELETE_WINDOW", on_closing)
tk.Label(root, text="输入1表示开始国战托管；输入2表示开始军争8人托管（刷主公、内奸），输入3表示军争5人托管（刷忠臣、反贼）", 
         fg="blue", font=('Arial', 10)).grid(row=0, column=0, columnspan=2, pady=5)
tk.Label(root, text="模式选择:").grid(row=1, column=0, sticky='nw', pady=5)
pos_entity = tk.Entry(root, width=50)
pos_entity.grid(row=1, column=1, padx=5, pady=5)
pos_entity.insert(0,str(pos))

tk.Label(root, text="循环次数:").grid(row=2, column=0, sticky='nw', pady=5)
count_entity = tk.Entry(root, width=50)
count_entity.grid(row=2, column=1, padx=5, pady=5)
count_entity.insert(0,str(count))
# 保存按钮
# 添加行提示
tk.Label(root, text="如果不想每次都改配置，可在代码12-13行修改，这样就会先加载你的设定", 
         fg="blue", font=('Arial', 10)).grid(row=3, column=0, columnspan=2, pady=5)
save_btn = tk.Button(root, text="三国杀启动！", command=save_config)
save_btn.grid(row=3, column=3, pady=10, sticky='e')

root.mainloop()

if pos == 1:
    guozhan()
elif pos==2:
    junzheng8()
elif pos==3:
    junzheng5()
